package com.macro.mall.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.macro.mall.common.exception.Assert;
import com.macro.mall.mgb.mapper.CartItemMapper;
import com.macro.mall.mgb.model.CartItem;
import com.macro.mall.mgb.model.Member;
import com.macro.mall.portal.domain.CartPromotionItem;
import com.macro.mall.portal.service.CartItemService;
import com.macro.mall.portal.service.MemberService;
import com.macro.mall.portal.service.OmsPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangwang
 * @version 1.0
 * @description 用户购物车管理
 * @date 2025/7/31 16:36
 */
@Service
public class CartItemServiceImpl implements CartItemService {

    @Autowired
    private CartItemMapper cartItemMapper;

    @Autowired
    private MemberService memberService;

    @Autowired
    private OmsPromotionService omsPromotionService;

    @Override
    @Transactional
    public int add(CartItem cartItem) {
        int count;
        Member member = memberService.getCurrentMember();
        cartItem.setMemberId(member.getId());
        cartItem.setMemberNickname(member.getNickname());
        cartItem.setDeleteStatus(0);
        CartItem cart = cartItemMapper.selectById(cartItem.getId());
        if (cart == null) {
            cartItem.setCreateDate(new Date());
            count = cartItemMapper.insert(cartItem);
        } else {
            cart.setModifyDate(new Date());
            cart.setQuantity(cart.getQuantity() + cartItem.getQuantity());
            count = cartItemMapper.updateById(cart);
        }
        return count;
    }

    @Override
    public List<CartItem> list(Long id) {
        return cartItemMapper.selectList(Wrappers.<CartItem>lambdaQuery()
                .eq(CartItem::getMemberId, id)
                .eq(CartItem::getDeleteStatus, 0));
    }


    @Override
    public List<CartPromotionItem> listPromotion(Long id, List<Long> cartIds) {
        List<CartItem> cartItemList = list(id);
        if (CollectionUtils.isEmpty(cartItemList)) {
            Assert.fail("购物车没有商品");
        }
        if (CollUtil.isNotEmpty(cartIds)) {
            cartItemList = cartItemList.stream().filter(item -> cartIds.contains(item.getId())).collect(Collectors.toList());
        }
        List<CartPromotionItem> cartPromotionItemList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(cartItemList)) {
             cartPromotionItemList = omsPromotionService.calcCartPromotion(cartItemList);
        }
        return cartPromotionItemList;
    }

    @Override
    public int updateQuantity(Long id, Long memberId, Integer quantity) {
        CartItem cartItem = cartItemMapper.selectOne(Wrappers.<CartItem>lambdaQuery()
                .eq(CartItem::getMemberId, memberId)
                .eq(CartItem::getId, id));
        cartItem.setQuantity(quantity);
        return cartItemMapper.updateById(cartItem);
    }

    @Override
    public int delete(Long memberId, List<Long> ids) {
        CartItem record = new CartItem();
        record.setDeleteStatus(1);
        LambdaUpdateWrapper<CartItem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CartItem::getMemberId, memberId)
                .in(CartItem::getId, ids);
        return cartItemMapper.update(record, updateWrapper);
    }

    @Override
    public int clear(Long memberId) {
        CartItem record = new CartItem();
        record.setDeleteStatus(1);
        LambdaUpdateWrapper<CartItem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CartItem::getMemberId, memberId);
        return cartItemMapper.update(record, updateWrapper);
    }

}
